package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.florist.FloristSecretary;
import com.motu.monstercity.server.game.commondata.florist.FloristSecretaryModel;
import com.motu.monstercity.server.game.commondata.florist.FloristTask;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class FloristBaseData {
    private static Logger logger = LoggerFactory.getLogger(FloristBaseData.class);

    public static void initData(List<Future> futures) {
        futures.add(initFloristSecretary());
        futures.add(initFloristTask());
    }

    private static ConcurrentHashMap<Integer, FloristSecretary> floristSecretaryMap = new ConcurrentHashMap<>(); // 花店秘书配置表

    /**
     * 初始化花店秘书配置表
     */
    public static Future<Void> initFloristSecretary() {
        String table = "florist_secretary";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, FloristSecretary> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        FloristSecretary entity = new FloristSecretary(result.getJsonObject(i));
                        tmp.put(entity.getSecretaryId(), entity);
                    }
                    floristSecretaryMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static FloristSecretary getFloristSecretary(int id) {
        FloristSecretary result = floristSecretaryMap.get(id);
        if (result == null) {
            logger.error("getFloristSecretary error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, FloristSecretary> getFloristSecretaryMap() { return floristSecretaryMap; }

    // 获取下一个秘书
    public static FloristSecretary getNextFloristSecretary(int secretaryId) {
        FloristSecretary secretary = getFloristSecretary(secretaryId);
        if (secretary == null) {
            return null;
        }
        List<FloristSecretary> secretaryList = new ArrayList<>(floristSecretaryMap.values());
        secretaryList.sort(Comparator.comparing(FloristSecretaryModel::getSort));
        int index = secretaryList.indexOf(secretary);
        if (index == secretaryList.size() - 1 || index >= secretaryList.size() - 1) {
            return null;
        }
        return secretaryList.get(index + 1);
    }

    private static ConcurrentHashMap<Integer, FloristTask> floristTaskMap = new ConcurrentHashMap<>(); // 花店任务配置表
    private static ConcurrentHashMap<Integer, List<FloristTask>> floristTaskTypeMap = new ConcurrentHashMap<>();


    /**
     * 初始化花店任务配置表
     */
    public static Future<Void> initFloristTask() {
        String table = "florist_task";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, FloristTask> tmp = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, List<FloristTask>> tmpType = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        FloristTask entity = new FloristTask(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                        tmpType.computeIfAbsent(entity.getTaskType(), k -> new ArrayList<>()).add(entity);
                    }
                    floristTaskMap = tmp;
                    floristTaskTypeMap = tmpType;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static FloristTask getFloristTask(int id) {
        FloristTask result = floristTaskMap.get(id);
        if (result == null) {
            logger.error("getFloristTask error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, FloristTask> getFloristTaskMap() { return floristTaskMap; }

    public static List<FloristTask> getFloristTaskListByType(int taskType) {
        List<FloristTask> result = floristTaskTypeMap.get(taskType);
        if (result == null) {
            logger.error("getFloristTaskList error key = " + taskType);
        }
        return result;
    }

    public static FloristTask getNextFloristTask(int id) {
        FloristTask task = getFloristTask(id);
        if (task == null) {
            return null;
        }
        List<FloristTask> taskList = new ArrayList<>(floristTaskMap.values());
        taskList.sort(Comparator.comparing(FloristTask::getSort));
        int index = taskList.indexOf(task);
        if (index == -1 || index >= taskList.size() - 1) {
            return null;
        }
        return taskList.get(index + 1);
    }
}
